/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.ifinance;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.xfok.http.client.HttpClient;
import net.xfok.string.filter.Filter;
import net.xfok.string.filter.Rule;
import android.os.Environment;

/**
 * @author YangHua
 *
 */
public class InfoConstant {
	public final static String INFO_TYPE="ifinance";
	public final static String RSS_URL="http://finance.ifeng.com/rss/headnews.xml";
	public final static String CHARSET="utf-8";
	public final static String ENCODE="utf-8";
	public final static String START="id=artical_real>";
	public final static String END="<!--热点推荐 begin-->";
	public final static int ALARM_TIME=15*60*1000;
	public final static int PAGE_SIZE=5;
	public final static int DURATION=500;
	public final static HttpClient CLIENT=new HttpClient();
	public final static String XFOK_PATH="/sdcard/xfok/";
	public final static String INFO_PATH=XFOK_PATH+"infos/";
	public final static String SAVE_PATH=INFO_PATH+INFO_TYPE+File.separator;
	public final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static int SAVE_SUC=1;
	public final static int NO_SDCARD=0;
	public final static int SAVE_EX=2;
	public final static int FORDER_LEN=5;
	public final static String FILE_EXT=".txt";
	public static final int STATUS_NEW=0;
	public static final int STATUS_DEL=1;
	public static final int STATUS_READ=2;
	public static final int STATUS_FAV=3;
	public static final String ACTION_UPDATE_WIDGET="net.xfok.info."+INFO_TYPE+"."+"update";
	public final static String INFO_SERVICE = "net.xfok.info."+INFO_TYPE+".SERVICE";
	private static final int BUFFER_SIZE=4096;
	
	public static String genHtml(String url){
		String html=null;
		try{
			html=filter(CLIENT.request(url, CHARSET));		
		}catch(Exception e){
			e.printStackTrace();
		}
		return html;
	}
	
	private static String filter(String html){
		Rule rule=new Rule();
		rule.setStartStr(START);
		rule.setEndStr(END);
		html=Filter.filter(html, rule);
		return html;
	}
	
	public static int saveHtml(String infoId,String html){
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			mkdir();
			String filePath=SAVE_PATH+infoId.substring(0,FORDER_LEN)+File.separator;
			String fileName=infoId+FILE_EXT;
	        if(!new File(filePath).exists()){
	        	new File(filePath).mkdir();
	        }
	        File saveFile=new File(filePath+fileName);
	        try{
		        FileOutputStream outStream = new FileOutputStream(saveFile);
		        outStream.write(html.getBytes());
		        outStream.close();
		        return SAVE_SUC;
	        }catch(Exception e){	        	
	        	e.printStackTrace();
	        	return SAVE_EX;
	        }
		}else{
			return NO_SDCARD;
		}
	}
	
	public static boolean checkHtml(String infoId){
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			String filePath=SAVE_PATH+infoId.substring(0,FORDER_LEN)+File.separator;
			String fileName=infoId+FILE_EXT;
			return new File(filePath+fileName).exists();
		}
		return true;
	}
	
	public static String readHtml(String infoId){
		String html=null;
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			String filePath=SAVE_PATH+infoId.substring(0,FORDER_LEN)+File.separator;
			String fileName=infoId+FILE_EXT;
			File readFile=new File(filePath+fileName);
			try{
				FileInputStream is=new FileInputStream(readFile);
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						is, CHARSET));
				StringBuffer b=new StringBuffer(BUFFER_SIZE);
				String line;
				while ((line = reader.readLine()) != null) {
					b.append(line);
					b.append("\r\n");
				}
				html=b.toString();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return html;
	}
	
	private static void mkdir(){
		 if(!new File(XFOK_PATH).exists()){
	         new File(XFOK_PATH).mkdir();
	     }
		 if(!new File(INFO_PATH).exists()){
	         new File(INFO_PATH).mkdir();
	     }
		 if(!new File(SAVE_PATH).exists()){
	         new File(SAVE_PATH).mkdir();
	     }
	}

	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	
	public static String dateToStr(Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(dateDate);
		return dateString;
	}
	
	public static String getNextDay(String now, int delay) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String mdate = "";
			Date d = strToDate(now);
			long myTime = (d.getTime() / 1000) + delay * 24 * 60 * 60;
			d.setTime(myTime * 1000);
			mdate = format.format(d);
			return mdate;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static boolean isBigger(String date1,String date2){
		return strToDate(date1).getTime()>strToDate(date2).getTime();
	}
}
